package  com.rrs.cost.record.service;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rrs.cost.equipment.CostEquipment;
import com.rrs.cost.equipment.service.ICostEquipmentService;
import  com.rrs.cost.record.CostRecord;
import  com.rrs.cost.record.dao.ICostRecordDao;
import com.rrs.cost.verifylog.CostVerifyLog;
import com.rrs.cost.verifylog.service.ICostVerifyLogService;

import org.rk.core.auth.util.SecurityUtil;
import org.rk.core.common.constant.RkConst;
import org.rk.core.common.util.RKDateUtil;
import org.rk.core.pub.base.service.ModelService;
import org.rk.core.sys.dictionary.CoreSysDictionary;
import org.rk.core.sys.dictionary.service.ICoreSysDictionaryService;


@Service("CostRecordService")
public class CostRecordService extends ModelService<CostRecord> implements ICostRecordService{
	
	private static final String costTypeDic = "cblxwh";
	private static final String equTypeDic = "equipmentType";
	private static final String currentVerifyStatusDic = "currentVerifyStatusDic";
	
	@Resource(name="CoreSysDictionaryService")
	private ICoreSysDictionaryService coreSysDictionaryService;
	@Resource(name="CostEquipmentService")
	private ICostEquipmentService costEquipmentService;
	@Resource(name="CostVerifyLogService")
	private ICostVerifyLogService costVerifyLogService;
	
	private ICostRecordDao modelDao;
	@Resource(name="CostRecordDao")
	public void setCostRecordDao(ICostRecordDao modelDao) {
		super.setDbDao(modelDao);
		this.modelDao=modelDao;
	}
	@Override
	public boolean updateCalcModel(CostRecord costRecord) {
		//计算折算金额
		costRecord.setCostSum(calcCostSum(costRecord));
		return super.updateModel(costRecord);
	}
	@Override
	public Long insertModel(CostRecord costRecord) {
		//计算折算金额
		costRecord.setCostSum(calcCostSum(costRecord));
		//初始审核状态--等待提交
		CoreSysDictionary currStausDic = coreSysDictionaryService.selectListByDicCodeAndDicValue(currentVerifyStatusDic,"1");
		costRecord.setCurrentStatus(currStausDic.getName());
		return super.insertModel(costRecord);
	}
	private String calcCostSum(CostRecord costRecord) {
		String costType = costRecord.getCostType();
		String equType = costRecord.getEquType();
		
		//读取字典配置的折算比例
		CoreSysDictionary costDic = coreSysDictionaryService.selectListByDicCodeAndDicValue(costTypeDic, costType);
		CoreSysDictionary equDic = coreSysDictionaryService.selectListByDicCodeAndDicValue(equTypeDic, equType);
		
		//获取设备数量
		Long equCount = 0L;
		List<CostEquipment> equList = costEquipmentService.selectListByEquType(equType, costRecord.getRegionCode());
		for (CostEquipment costEquipment : equList) {
			Integer equNum = costEquipment.getInstallNum();
			equCount = equCount + equNum;
		}
		
		String costRate = costDic.getBakField2();
		String costIsAl = costDic.getBakField1();
		
		String equRate = equDic.getBakField2();
		String equIsAl = equDic.getBakField1();
		
		BigDecimal costAmont = new BigDecimal(costRecord.getCostNum()).multiply(new BigDecimal(costRate));
		BigDecimal equAmont = new BigDecimal(equCount).multiply(new BigDecimal(equRate));
		
		BigDecimal costSum = new BigDecimal("0");
		if("是".equals(costIsAl)) {
			costSum = costSum.subtract(costAmont);
		}else {
			costSum = costSum.add(costAmont);
		}
		if("是".equals(equIsAl)) {
			costSum = costSum.subtract(equAmont);
		}else {
			costSum = costSum.add(equAmont);
		}
		return costSum.toPlainString();
	}
	@Override
	@Transactional
	public boolean deleteById(Long id){
		return modelDao.deleteById(id);
	}
	
	@Override
	public boolean verify(Long id,String currentStatus) {
		//读取审核状态
		CoreSysDictionary currStausDic = coreSysDictionaryService.selectListByDicCodeAndDicValue(currentVerifyStatusDic,currentStatus);
		CostRecord costRecord = super.selectModel(id);
		costRecord.setCheckTime(RKDateUtil.getCurrentTimestamp());
		costRecord.setCheckor(SecurityUtil.getUserName());
		costRecord.setCurrentStatus(currStausDic.getName());
		costRecord.setUpdator(SecurityUtil.getUserName());
		//创建审核日志
		CostVerifyLog verifyLog = new CostVerifyLog();
		verifyLog.setRecordId(id);
		verifyLog.setCreator(SecurityUtil.getUserName());
		verifyLog.setCreateTime(RKDateUtil.getCurrentTimestamp());
		verifyLog.setVerifyStatus(currStausDic.getName());
		verifyLog.setEnabled(RkConst.yesno.yes);
		verifyLog.setVerifyor(SecurityUtil.getUserName());
		costVerifyLogService.insertModel(verifyLog);
		return super.updateModel(costRecord);
	}
}
